##########################################################################################

library(data.table)
library(optparse)
library(dplyr)
library(ggplot2)
library(ggsci)
library(RColorBrewer)
library(ggpubr)

##########################################################################################

option_list <- list(
    make_option(c("--njmu_clinic_file"), type = "character") ,
    make_option(c("--tcga_clinic_file"), type = "character") ,
    make_option(c("--oncoSG_clinic_file"), type = "character") ,
    make_option(c("--tmucih_clinic_file"), type = "character") ,
    make_option(c("--input_file"), type = "character") ,
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    
    njmu_clinic_file <- "~/20220915_gastric_multiple/dna_combinePublic/baseTable/STAD_Info.addBurden.MSI_MSS.addCNVType.tsv"
    tcga_clinic_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/TCGA/stad_tcga_pan_can_atlas_2018_clinical_data.tsv"
    oncoSG_clinic_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/OncoSG/stad_oncosg_2018_clinical_data.tsv"
    tmucih_clinic_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/TMUCIH/egc_tmucih_2015_clinical_data.tsv"
    input_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/combine/MutationInfo.combine.tsv"

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

input_file <- opt$input_file
tcga_clinic_file <- opt$tcga_clinic_file
oncoSG_clinic_file <- opt$oncoSG_clinic_file
tmucih_clinic_file <- opt$tmucih_clinic_file
njmu_clinic_file <- opt$njmu_clinic_file
out_path <- opt$out_path

dir.create(out_path , recursive = T)

###########################################################################################

dat_input <- data.frame(fread(input_file , quote=""))
dat_njmu_clinic <- data.frame(fread(njmu_clinic_file , quote=""))
dat_tcga_clinic <- data.frame(fread(tcga_clinic_file , quote=""))
dat_oncosg_clinic <- data.frame(fread(oncoSG_clinic_file , quote=""))
dat_tmucih_clinic <- data.frame(fread(tmucih_clinic_file , quote=""))

tmp_njmu <- dat_njmu_clinic[ , c("Patient" , "TCGA_Class") ]
tmp_njmu <- subset( tmp_njmu , TCGA_Class!="IM" ) 
tmp_njmu <- tmp_njmu %>%
group_by( Patient ) %>%
summarize( TCGA_Class = paste0(sort(unique(TCGA_Class)) , collapse = "|") )

dat_tmucih_clinic$Molecular.subtype <- ifelse( dat_tmucih_clinic$EBV.Status == "Positive" , "EBV" , "unknown" )
dat_tmucih_clinic$Molecular.subtype <- ifelse( dat_tmucih_clinic$TMB..nonsynonymous. > 20 , "POLE" , dat_tmucih_clinic$Molecular.subtype  )

tmp_tcga <- dat_tcga_clinic[ , c("Patient.ID" , "Subtype") ]
tmp_oncosg <- dat_oncosg_clinic[ , c("Patient.ID" , "Molecular.subtype") ]
tmp_tmucih <- dat_tmucih_clinic[ , c("Patient.ID" , "Molecular.subtype") ]

###########################################################################################

col_names <- c("Patient.ID" , "Molecular.subtype")
colnames(tmp_njmu) <- col_names
colnames(tmp_tcga) <- col_names
colnames(tmp_oncosg) <- col_names
colnames(tmp_tmucih) <- col_names

dat <- rbind( tmp_njmu , tmp_tcga , tmp_oncosg , tmp_tmucih )

###########################################################################################

result <- merge( dat_input , dat , by.x = "Tumor" , by.y = "Patient.ID" , all.x = T )
result$Molecular.subtype[is.na(result$Molecular.subtype)] <- "unknown"
result$Molecular.subtype <- gsub( "STAD_" , "" , result$Molecular.subtype)

###########################################################################################

result[result$MS_Type=="MSI" & result$Molecular.subtype == "unknown" , "Molecular.subtype"] <- "MSI"

out_name <- paste0(out_path , "/MutationInfo.combine.addMolecularSubType.tsv")
write.table( result , out_name , row.names = F , quote = F , sep = "\t" )